package vn.bang.xiangqi.components;

import java.util.Collection;

import vn.bang.xiangqi.infrastructure.Position;
import vn.bang.xiangqi.infrastructure.Vector;
import vn.bang.xiangqi.utility.BoardUtil;

public class Horse extends AbstractBaseNode {
	public Horse() {
		super();
	}

	public Horse(Position originalPosition) {
		super(originalPosition);
	}

	private static Vector[] HORSE_VECTORS = { new Vector(-1, -2),
			new Vector(+1, -2), new Vector(+2, -1), new Vector(+2, +1),
			new Vector(+1, +2), new Vector(-1, +2), new Vector(-2, +1),
			new Vector(-2, -1) };

	@Override
	protected boolean notPrevented(Vector vector, Collection<INode> ours,
			Collection<INode> enemies) {
		Position blockingPosition = new Position(position.getRowIndex()
				+ (int) (vector.getRow() / 2), position.getColumnIndex()
				+ (int) (vector.getColumn() / 2));

		for (INode node : ours) {
			if (node.PositionAt(blockingPosition)) {
				return false;
			}
		}

		for (INode enemy : enemies) {
			if (enemy.PositionAt(blockingPosition)) {
				return false;
			}
		}

		return true;
	}

	@Override
	protected Vector retrieveVectorMovingTo(Position destinationPosition) {
		if (null == destinationPosition
				|| !BoardUtil.IsInBoard(destinationPosition)) {
			return null;
		}

		return retrieveVectorValid(destinationPosition, HORSE_VECTORS);
	}

}